package com.yh.threadTest.pojo;

/**
 * @ClassName RaceThread
 * @Description TODO
 * @Author 0715-YuHao
 * @Date 2020/11/14 9:52
 */
public class RaceThread implements Runnable {

    private static String winner;

    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
//            if (Thread.currentThread().getName().equals("兔子") && i % 10 == 0) {
//                try {
//                    Thread.sleep(1);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
            if (gameOver(i)) {
                break;
            }
            System.out.println(Thread.currentThread().getName() + "跑了" + i + "米");
        }
    }

    public boolean gameOver(int steps) {
        if (winner != null) {
            return true;
        }else if (steps == 100) {
            winner = Thread.currentThread().getName();
            System.out.println(winner + "获胜了");
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        RaceThread r = new RaceThread();
        new Thread(r, "兔子").start();
        new Thread(r, "乌龟").start();
    }

}
